package com.ruoyi.framework.shiro.web;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.apache.shiro.web.util.WebUtils;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

public class ShiroLoginFilter extends FormAuthenticationFilter {

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        Subject subject = getSubject(request, response);

        if (subject.getPrincipal() == null) {
            HttpServletRequest httpServletRequest = (HttpServletRequest) request;
            HttpServletResponse httpServletResponse = (HttpServletResponse) response;
            String url = httpServletRequest.getRequestURI();
            //if (isAjaxRequest(httpServletRequest) || url.equals("/common/upload")) {
            if (isAjaxRequest(httpServletRequest)||isMultipartContent(httpServletRequest)) {
                httpServletResponse.setContentType("application/json; charset=utf-8");

                Map<String, Object> response1 = new HashMap<String, Object>();
                response1.put("code", 401);
                response1.put("msg", "未登录或者登录已失效");

                String json = JSON.toJSONString(response1);

                httpServletResponse.getWriter().write(json);
                //AjaxResult.error("未登录或者登录已失效");
            } else {
                /************************/
                //httpServletResponse.setContentType("text/html;charset=UTF-8");
                //httpServletResponse.getWriter().write("<script>window.alert('未登录或登录超时。请重新登录');window.location='/login'</script>");
                //return false;
                /**********************/
                WebUtils.issueRedirect(request,response,getLoginUrl(),null,true,false);
                // 如果不是异步请求，执行默认的重定向行为
                return super.onAccessDenied(request, response);

            }
            return false;
        }
        return  true;
    }

    private boolean isAjaxRequest(HttpServletRequest request) {
        String accept = request.getHeader("Accept");
        if (accept != null && accept.contains("application/json")) {
            return true;
        }
        String xRequestedWith = request.getHeader("X-Requested-With");
        return "XMLHttpRequest".equals(xRequestedWith);
    }
    private boolean isMultipartContent(HttpServletRequest request) {
        String contentType = request.getContentType();
        return contentType != null && contentType.toLowerCase().startsWith("multipart/form-data");
    }
    // 跳转到登录页
    @Override
    protected void redirectToLogin(ServletRequest request, ServletResponse response) throws IOException
    {

        WebUtils.issueRedirect(request, response, getLoginUrl(),null,true,false);
    }
}